package com.core.shared;

import com.core.shared.http.HttpUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-6-17
 * Time: 下午3:08
 * To change this template use File | Settings | File Templates.
 */
public class CityHandle {

    public static Object[] getLngLatByCity(String addr) throws IOException {
        String lng = null;// 经度
        String lat = null;// 纬度
        String address = null;
        try {
            address = java.net.URLEncoder.encode(addr, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        String key = "7ybG8YcIEHwa5DfFAYdg01Gw";
        String url = String.format("http://api.map.baidu.com/geocoder?address=%s&output=json&key=%s", address, key);
        URL myURL = null;
        URLConnection httpsConn = null;
        try {
            myURL = new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        InputStreamReader insr = null;
        BufferedReader br = null;
        try {
            httpsConn = myURL.openConnection();// 不使用代理
            if (httpsConn != null) {
                insr = new InputStreamReader(httpsConn.getInputStream(), "UTF-8");
                br = new BufferedReader(insr);
                String data;
                int count = 1;
                while ((data = br.readLine()) != null) {
                    if (count == 5) {
                        lng = (String) data.subSequence(data.indexOf(":") + 1, data.indexOf(","));// 经度
                        count++;
                    } else if (count == 6) {
                        lat = data.substring(data.indexOf(":") + 1);// 纬度
                        count++;
                    } else {
                        count++;
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (insr != null) {
                insr.close();
            }
            if (br != null) {
                br.close();
            }
        }
        return new Object[]{lng, lat};
    }

    public static Map<String, String> getMapByLngLat(String lng, String lat) {
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=" + lat + "," + lng + "&output=json";
        String str = HttpUtil.connectURL(url);
        Map<?, ?> map = JsonUtil.toObject(str, Map.class);
        Map<String, String> address = (Map<String, String>) ((Map<?, ?>) map.get("result")).get("addressComponent");
        return address;
    }

    public static String getAddressByLngLat(String lng, String lat) {
        String url = "http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=" + lat + "," + lng + "&output=json";
        String str = HttpUtil.connectURL(url);
        Map<?, ?> map = JsonUtil.toObject(str, Map.class);
        String address = ((Map<?, ?>) map.get("result")).get("formatted_address").toString();
        return address;
    }



}
